package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsHomeBrand;
import cn.zcscloud.mall.service.SmsHomeBrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页品牌管理
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/home/brand")
public class SmsHomeBrandController {
    @Autowired
    private SmsHomeBrandService smsHomeBrandService;

    /**
     * 添加首页推荐品牌
     *
     * @param homeBrandList 推荐品牌列表
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody List<SmsHomeBrand> homeBrandList) {
        int count = smsHomeBrandService.updateList(homeBrandList);
        //boolean b = smsHomeBrandService.saveBatch(homeBrandList);
        if (count == 0) return R.error("添加品牌均已存在");
        return R.success("成功添加"+count+"条新数据",null);
    }

    @PostMapping("/delete")
    public R update(@RequestParam("ids") List<Long> ids) {
        boolean b = smsHomeBrandService.removeByIds(ids);
        if (!b) return R.error();
        return R.success("");
    }

    @GetMapping("/list")
    public R list(String brandName,
                  String recommendStatus,
                  @RequestParam(defaultValue = "5") Integer pageSize,
                  @RequestParam(defaultValue = "1") Integer pageNum) {
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(brandName != null, SmsHomeBrand::getBrandName, brandName)
                .eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);
        Page<SmsHomeBrand> iPage = smsHomeBrandService.page(page, lqw);
        return R.success(PageRequest.init(iPage));
    }

    /**
     * 批量修改推荐品牌状态
     *
     * @param ids             品牌id
     * @param recommendStatus 推荐状态
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R updateStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus) {
        SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
        smsHomeBrand.setRecommendStatus(recommendStatus);
        int count = 0;
        for (Long id : ids) {
            smsHomeBrand.setId(id);
            smsHomeBrandService.updateById(smsHomeBrand);
            count++;
        }
        if (count == 0) return R.error();

        return R.success(null);
    }

    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public R updateSort(@PathVariable Long id, Integer sort){
        SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
        smsHomeBrand.setId(id);
        smsHomeBrand.setSort(sort);
        boolean b = smsHomeBrandService.updateById(smsHomeBrand);
        if (!b) return R.error();
        return R.success(null);
    }

}
